package com.xique.park.bean.transform.web.garageLaneDev;

import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.ParkGarage;
import com.xique.park.bean.domain.ParkLane;
import com.xique.park.bean.request.web.garageLaneDev.lane.AddLaneRequest;
import com.xique.park.bean.request.web.garageLaneDev.lane.EditLaneRequest;
import com.xique.park.bean.vo.web.garageLaneDeviceTree.TreeNodeLaneVo;
import com.xique.park.constant.DictConstant;

public class LaneTransform {

    /**
     * 通道实体转化为Vo实体
     */
    public static TreeNodeLaneVo transformNodeLaneVo(ParkLane parkLane, ParkGarage parkGarage) {
        TreeNodeLaneVo treeLaneVo = new TreeNodeLaneVo();
        treeLaneVo.setGarageId(parkLane.getGarageId());
        treeLaneVo.setGarageName(parkGarage.getName());
        treeLaneVo.setLaneType(parkLane.getLaneType());
        treeLaneVo.setInGarageId(parkLane.getInGarageId());
        treeLaneVo.setOutGarageId(parkLane.getOutGarageId());
        treeLaneVo.setIsQrcode(parkLane.getIsQrcode());
        treeLaneVo.setModel(parkLane.getModel());
        treeLaneVo.setSamePlateInterval(parkLane.getSamePlateInterval());
        treeLaneVo.setName(parkLane.getName());
        treeLaneVo.setType("lane");
        treeLaneVo.setCreateTime(parkLane.getCreateTime());
        treeLaneVo.setUpdateTime(parkLane.getUpdateTime());
        //一部分配置在remark
        String remark = parkLane.getRemark();
        if (StringUtils.isNotEmpty(remark)) {
            JSONObject remarkObj = JSONObject.parseObject(remark);
            treeLaneVo.setInternetType(remarkObj.getString("internetType"));
            treeLaneVo.setPicPixel(remarkObj.getString("picPixel"));
            treeLaneVo.setIsRemoteVideo(remarkObj.getString("isRemoteVideo"));
        }
        //一部分配置在config
        String config = parkLane.getConfig();
        if (StringUtils.isNotEmpty(config)) {
            JSONObject configObj = JSONObject.parseObject(config);
            treeLaneVo.setIsFake(configObj.getString("isFake"));
            treeLaneVo.setFakeTime(configObj.getString("fakeTime"));
            treeLaneVo.setIsFreeScan(configObj.getString("isFreeScan"));
            treeLaneVo.setIsPayPass(configObj.getString("isPayPass"));
        }
        return treeLaneVo;
    }

    /**
     * 添加通道request转化为lane实体
     */
    public static ParkLane transformAddLaneRequest(AddLaneRequest request) {
        ParkLane parkLane = new ParkLane();
        parkLane.setName(request.getName());
        parkLane.setGarageId(request.getGarageId());
        if (StringUtils.isNotNull(request.getInGarageId())) {
            parkLane.setInGarageId(request.getInGarageId());
        } else {
            if (DictConstant.LANE_TYPE_INSIDEIN.equals(request.getLaneType()) || DictConstant.LANE_TYPE_INSIDEOUT.equals(request.getLaneType())) {
                parkLane.setInGarageId(request.getGarageId());
            }
        }
        parkLane.setOutGarageId(request.getOutGarageId());
        parkLane.setLaneType(request.getLaneType());
        parkLane.setModel(request.getModel());
        parkLane.setSamePlateInterval(request.getSamePlateInterval());
        JSONObject remarkObj = new JSONObject();
        remarkObj.put("internetType", request.getInternetType());
        remarkObj.put("picPixel", request.getPicPixel());
        remarkObj.put("isRemoteVideo", request.getIsRemoteVideo());
        parkLane.setRemark(remarkObj.toJSONString());
        parkLane.setIsQrcode(request.getIsQrcode());
        JSONObject configObj = new JSONObject();
        configObj.put("isFake", request.getIsFake());
        configObj.put("fakeTime", request.getFakeTime());
        configObj.put("isFreeScan", request.getIsFreeScan());
        configObj.put("isPayPass", request.getIsPayPass());
        parkLane.setConfig(configObj.toJSONString());
        parkLane.setParkId(SecurityUtils.getParkId());
        return parkLane;
    }

    /**
     * 编辑通道request转化为lane实体
     */
    public static ParkLane transformEditLaneRequest(EditLaneRequest request) {
        ParkLane parkLane = new ParkLane();
        parkLane.setId(request.getId());
        parkLane.setName(request.getName());
        parkLane.setGarageId(request.getGarageId());
        if (StringUtils.isNotNull(request.getInGarageId())) {
            parkLane.setInGarageId(request.getInGarageId());
        } else {
            if (DictConstant.LANE_TYPE_INSIDEIN.equals(request.getLaneType()) || DictConstant.LANE_TYPE_INSIDEOUT.equals(request.getLaneType())) {
                parkLane.setInGarageId(request.getGarageId());
            }
        }
        parkLane.setOutGarageId(request.getOutGarageId());
        parkLane.setLaneType(request.getLaneType());
        parkLane.setModel(request.getModel());
        parkLane.setSamePlateInterval(request.getSamePlateInterval());
        JSONObject remarkObj = new JSONObject();
        remarkObj.put("internetType", request.getInternetType());
        remarkObj.put("picPixel", request.getPicPixel());
        remarkObj.put("isRemoteVideo", request.getIsRemoteVideo());
        parkLane.setRemark(remarkObj.toJSONString());
        parkLane.setIsQrcode(request.getIsQrcode());
        JSONObject configObj = new JSONObject();
        configObj.put("isFake", request.getIsFake());
        configObj.put("fakeTime", request.getFakeTime());
        configObj.put("isFreeScan", request.getIsFreeScan());
        configObj.put("isPayPass", request.getIsPayPass());
        parkLane.setConfig(configObj.toJSONString());
        parkLane.setParkId(SecurityUtils.getParkId());
        return parkLane;
    }

}
